package com.yuexunit.finance.member.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.member.entity.MonthEarningEntity;
import com.yuexunit.finance.member.entity.lexicon.RepayStateEnum;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.StringUtil;

@Implements(MonthEarningService.class)
public class MonthEarningServiceImpl implements MonthEarningService {

	@Override
	@Transactional
	public Long createMonthEarning(MonthEarningEntity entity) {
		entity.setCreateDt(DateUtils.now());
		entity.setUpdateDt(DateUtils.now());
		entity.setEnabled(Boolean.TRUE);
		entity.setRepayState(RepayStateEnum.NOREPAY);
		entity.setMonthEarningNumber(SettingConstants.PRE_NUMBER_MONTHS_EARNING + RandomUtil.generateTimeNumber());

		PersistUtil.insert(entity);
		return entity.getMonthEarningId();
	}

	@Override
	@Transactional
	public void disabledMonthEarning(Long monthEarningId) {
		MonthEarningEntity entity = PersistUtil.get(MonthEarningEntity.class, monthEarningId);
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}

	@Override
	public void updateMonthEarning(MonthEarningEntity entity) {
		entity.setUpdateDt(DateUtils.now());
		PersistUtil.update(entity);
	}

	@Override
	public MonthEarningEntity inquiryMonthEarningDetail(Long monthEarningId) {
		return PersistUtil.get(MonthEarningEntity.class, monthEarningId);
	}

	@Override
	public List<MonthEarningEntity> inquiryMonthEarningDetail(
			String monthEarningNumber, Long enterpriseId, Integer year, Integer month) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(monthEarningNumber)) {
			criteria.add(Restrictions.like("monthEarningNumber", "%" + monthEarningNumber + "%"));
		}
		if (enterpriseId !=null && enterpriseId > 0) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if(year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if(month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<MonthEarningEntity> inquiryMonthEarningPageList(
			Integer year, Integer month,
			String monthEarningNumber, String enterpriseName,
			Date earningDateStar, Date earningDateEnd, Integer confirmStatus, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(monthEarningNumber)) {
			criteria.add(Restrictions.like("monthEarningNumber", "%" + monthEarningNumber + "%"));
		}
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		if (earningDateStar != null && earningDateEnd != null) {
			criteria.add(Restrictions.between("earningDate", earningDateStar, earningDateEnd));
		}
		if (confirmStatus != null) {
			criteria.add(Restrictions.eq("confirmStatus", confirmStatus));
		}
		if(year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if(month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<MonthEarningEntity> inquiryMonthEarningList(
			Integer year, Integer month,
			String monthEarningNumber, String enterpriseName,
			Date earningDateStar, Date earningDateEnd) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(monthEarningNumber)) {
			criteria.add(Restrictions.like("monthEarningNumber", "%" + monthEarningNumber + "%"));
		}
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		if (earningDateStar != null && earningDateEnd != null) {
			criteria.add(Restrictions.between("earningDate", earningDateStar, earningDateEnd));
		}
		if(year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if(month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public List<MonthEarningEntity> inquiryMonthEarningList(Integer earningDate, String enterpriseName, Long enterpriseId,
			String monthEarningNumber, Integer year, Integer month) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(monthEarningNumber)) {
			criteria.add(Restrictions.like("monthEarningNumber", "%" + monthEarningNumber + "%"));
		}
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.eq("monthEarningNumber", monthEarningNumber));
		}
		if (earningDate != null) {
			criteria.add(Restrictions.eq("earningDate", DateUtils.fromDateInt(earningDate)));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (year != null) {
			criteria.add(Restrictions.eq("year", year));
		}
		if (month != null) {
			criteria.add(Restrictions.eq("month", month));
		}
		return PersistUtil.get(criteria);
	}
}
